<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\InfoBox;
use GuzzleHttp\Client as GuzzleClient;
use Encore\Admin\Facades\Admin;
use App\Models\Order;
use Illuminate\Support\Facades\Request;

class RiskController extends Controller
{
    public function index()
    {
        $userIdent = env('BSIGN');
        $get_url = env('RISKDECISION').'/risk/Config/GetNew';

        $params = [
            'headers' => [
                'ident' => 'production'
            ],
        ];

        $config_data = $this->guzzle($get_url, $params);
        $res = json_decode($config_data->getBody()->getContents(), true);
//        echo '<pre>';
//        var_dump($res);
//        die;
        return Admin::content(function (Content $content) use($res){
            $content->header('初审管理');
            $content->description('初审设置');
            $articleView = view('admin.risk', compact('res', 'score_res'))->render();
            $content->row($articleView);
            admin_toastr('提示', 'success');
        });
    }
    //疑似欺诈全部默认开启，状态拒绝
    protected function like_cheat($risk_id){
        $risk_item_list_two = [
            'risk_id' => $risk_id,
            'is_open' => 1,
            'value_one' => '',
            'value_two' => '',
            'check_type' => 1
        ];
        $this->config_default_save($risk_item_list_two);
    }
    //发送请求
    public function guzzle($url, $params)
    {
        $client = new GuzzleClient([
            // 超时
            'timeout' => 60.0,

        ]);
        return $client->request('GET', $url, $params);
    }

    public function guzzle_post($url, $params){
        $client = new GuzzleClient([
            // 超时
            'timeout' => 60.0,
        ]);
        return $client->request('POST', $url, $params);
    }
    //保存风控决策接口
    public function config_save(){
        $input = Request::all();
        $params = [
            'json' => [
                'risk_id' => $input['risk_id'] ? $input['risk_id'] : '',
                'is_open' => isset($input['is_open']) ? $input['is_open'] : '',
                'value_one' => isset($input['value_one']) ? $input['value_one'] : '',
                'value_two' => isset($input['value_two']) ? $input['value_two'] : '',
                'check_type' => isset($input['check_type']) ? $input['check_type'] : 0
            ],
            'headers' => [
                'ident' => 'production'
            ]
        ];

        $send_url = env('RISKDECISION').'/risk/Config/SaveNew';
        try{
            $res = $this->guzzle_post($send_url, $params);
            $return = json_decode($res->getBody()->getContents(), true);
            @file_put_contents('../storage/logs/risk_config_save.log', 'date='.date('Y-m-d H:i:s').'||post='.json_encode($params).'||res='.json_encode($return).PHP_EOL, FILE_APPEND);
            return $return;
        }catch (\Exception $exception){
            @file_put_contents('../storage/logs/risk_config_save_err_.log', 'date='.date('Y-m-d H:i:s').'||post='.json_encode($params).'||err='.$exception->getMessage().PHP_EOL, FILE_APPEND);
            return ['message' => '保存失败'];
        }

    }
    //保存默认值得接口
    protected function config_default_save($data){
        $params = [
            'json' => [
                'user_id' => env('BSIGN'),
                'risk_id' => $data['risk_id'] ? $data['risk_id'] : '',
                'is_open' => isset($data['is_open']) ? $data['is_open'] : 0,
                'value_one' => isset($data['value_one']) ? $data['value_one'] : '',
                'value_two' => isset($data['value_two']) ? $data['value_two'] : '',
                'check_type' => isset($data['check_type']) ? $data['check_type'] : 1
            ],
            'headers' => [
                'ident' => 'production'
            ]
        ];

        $send_url = env('RISKDECISION').'/easyuseapi/risk/config/save';
        try{
            $res = $this->guzzle_post($send_url, $params);
            $return = json_decode($res->getBody()->getContents(), true);
            @file_put_contents('../storage/logs/risk_config_default_save.log', 'date='.date('Y-m-d H:i:s').'||post='.json_encode($params).'||res='.json_encode($return).PHP_EOL, FILE_APPEND);
            return $return;
        }catch (\Exception $exception){
            @file_put_contents('../storage/logs/risk_config_default_save_err_.log', 'date='.date('Y-m-d H:i:s').'||post='.json_encode($params).'||err='.$exception->getMessage().PHP_EOL, FILE_APPEND);
        }
    }
    //保存三个方案的方法
    public function grade_config_save(){
        $input = Request::all();
        $params = [
            'json' => [
                'user_id' => env('BSIGN'),
                'score_config_guid' => empty($input['score_config_guid']) ? '' : $input['score_config_guid'],
                'risk_name' => empty($input['risk_name']) ? '' : $input['risk_name'],
                'ok_score' => empty($input['ok_score']) ? '' : $input['ok_score'],
                'deny_score' => empty($input['deny_score']) ? '' : $input['deny_score'],
                'base_score' => empty($input['base_score']) ? '' : $input['base_score'],
                'base_deny_score' => empty($input['base_deny_score']) ? '' : $input['base_deny_score'],
                'grade_name' => empty($input['grade_name']) ? '' : $input['grade_name'],
            ],
            'headers' => [
                'ident' => 'production'
            ]
        ];

        try{
            $grade_save_url = env('RISKDECISION').'/easyuseapi/risk/config/savescore';
            $res = $this->guzzle_post($grade_save_url, $params);
            $return = json_decode($res->getBody()->getContents(), true);
            @file_put_contents('../storage/logs/grade_config_save.log', 'date='.date('Y-m-d H:i:s').'||post='.json_encode($params).'||res='.json_encode($return).PHP_EOL, FILE_APPEND);
            return $return;
        }catch (\Exception $exception){
            @file_put_contents('../storage/logs/grade_config_save_err_.log', 'date='.date('Y-m-d H:i:s').'||post='.json_encode($params).'||err='.$exception->getMessage().PHP_EOL, FILE_APPEND);
            return ['message' => '保存失败'];
        }



    }

    public function get_score_config(){
        $userIdent = env('BSIGN');
        $params = [
            'headers' => [
                'ident' => 'production'
            ],
        ];
        $get_score_list_url = env('RISKDECISION').'/easyuseapi/risk/config/getscorelist?user_id='.$userIdent;
        $score_config_data = $this->guzzle($get_score_list_url, $params);
        return json_decode($score_config_data->getBody()->getContents(), true);
    }
}